package lingou.homework.postbar.tool;

import lingou.homework.postbar.domain.Post;
import lingou.homework.postbar.exception.SystemException;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.*;
import java.util.UUID;

public class StaticURL {
    public static final String HEAD_POOL = "image/head";
    public static final String POST_POOL = "image/post";
    public static final String IDENTIFY_POOL = "image/identify";

    private static void createDir(String path) {
        File head = new File(path);
        if (!head.exists()) head.mkdirs();
        if (!head.isDirectory()) throw new SystemException(path + " should be a directory");
    }

    static {
        createDir(HEAD_POOL);
        createDir(POST_POOL);
        createDir(IDENTIFY_POOL);
    }

    public static String getUserHome(long id) {
        return "/page/person?userId=" + id;
    }

    public static String getPostLink(long postId){
        return "/page/post?postId=" + postId;
    }

//    public static void toUserHead(long id, InputStream file) throws Exception {
//        File head = new File(HEAD + "/" + id);
//        if (!head.exists()) {
//            try {
//                head.createNewFile();
//            } catch (IOException e) {
//                throw new SystemException(e);
//            }
//        }
//        try (FileOutputStream output = new FileOutputStream(head)) {
//            IOUtils.copy(file, output);
//        }
//    }
//
//    public static InputStream getUserHead(long id) {
//        File head = new File(HEAD + "/" + id);
//        try {
//            if (!head.exists()) head = DEFAULT_HEAD;
//            return new FileInputStream(head);
//        } catch (FileNotFoundException e) {
//            throw new SystemException("头像文件未找到：" + head.getPath());
//        }
//    }


//    public static String toPostImage(long postId, InputStream file) throws IOException {
//        return saveToPool(file, POST + "/" + postId);
//    }
//
//    public static InputStream getPostImage(long postId, String name) throws FileNotFoundException {
//        return getFromPool(name, POST + "/" + postId);
//    }
//
//    public static void deletePostImage(long postId, String name) {
//        deleteFromPool(name, POST + "/" + postId);
//    }

    public static String saveToPool(InputStream file, String poolPath) throws IOException {
        String name = UUID.randomUUID().toString();
        saveToPool(file, poolPath, name);
        return name;
    }

    public static void saveToPool(InputStream file, String poolPath, String name) throws IOException {
        name = wrapName(name);
        try (FileOutputStream fileOutputStream = new FileOutputStream(poolPath + "/" + name)) {
            IOUtils.copy(file, fileOutputStream);
        }
    }

    public static InputStream getFromPool(String poolPath, String name) throws FileNotFoundException {
        name = wrapName(name);
        File file = new File(poolPath + "/" + name);
        if(!file.exists()) file = new File(poolPath + "/default");
        return new FileInputStream(file);
    }

    public static void deleteFromPool(String poolPath, String name) {
        name = wrapName(name);
        File file = new File(poolPath + "/" + name);
        file.delete();
    }

    private static String wrapName(String name){
        if(name.startsWith("default")) return name + "_";
        return name;
    }

}
